package cn.llming.auth.controller;


import cn.llming.auth.domain.Role;
import cn.llming.auth.query.RoleQuery;
import cn.llming.auth.service.IRoleService;
import cn.llming.auth.util.BasePermission;
import cn.llming.basic.query.BaseQuery;
import cn.llming.basic.result.BaseResult;
import cn.llming.dto.MenuDto;
import cn.llming.dto.RolePermissionDto;
import cn.llming.vo.result.AjaxResult;
import cn.llming.vo.result.ListPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@RequestMapping("/role")
@RestController
@BasePermission(name = "角色管理",desc = "角色管理权限")
public class RoleController {

    @Autowired
    private IRoleService roleService;
    //增加、修改
    @PostMapping
    @BasePermission(name = "增加、修改接口")
    public AjaxResult updateOradd(@RequestBody Role role){

        try {
            if(Objects.isNull(role.getId())){
                roleService.insert(role);
            }else {
                roleService.update(role);
            }
            return AjaxResult.myRes();

        }catch (Exception e){
            return AjaxResult.myRes().setMessage("保存失败"+e.getMessage()).setSuccess(false);
        }
    }
    //删除
    @DeleteMapping("/{id}")
    @BasePermission(name = "删除接口")
    public AjaxResult deleteRole(@PathVariable("id") Long id){

        try {
                roleService.delete(id);
            return AjaxResult.myRes();

        }catch (Exception e){
            return AjaxResult.myRes().setMessage("删除失败"+e.getMessage()).setSuccess(false);
        }
    }
    //查询单个

    @GetMapping("/{id}")
    @BasePermission(name = "查询单个接口")
    public AjaxResult selectRoleById(@PathVariable("id") String id){
        try {
            Role role=roleService.getById(Long.parseLong(id));
            return AjaxResult.myRes().setObject(role);
        }catch (Exception e){
            return AjaxResult.myRes().setMessage("查询单个失败"+e.getMessage()).setSuccess(false);
        }
    }
    //查询全部
    @GetMapping
    @BasePermission(name = "查询所有接口")
    public AjaxResult selectAll(){
        try {
            List<Role> roles=roleService.getAll();
            return AjaxResult.myRes().setObject(roles);
        }catch (Exception e){
            return AjaxResult.myRes().setMessage("查询所有失败"+e.getMessage()).setSuccess(false);
        }
    }

    //批量删除
    @PatchMapping
    @BasePermission(name = "批量删除接口")
    public AjaxResult patchDel(@RequestBody List<Long> ids){
        try {
         roleService.batchDelete(ids);
            return AjaxResult.myRes();
        }catch (Exception e){
            return AjaxResult.myRes().setMessage("批量删除失败"+e.getMessage()).setSuccess(false);
        }
    }

    //分页查询
    @PutMapping
    @BasePermission(name = "分页查询接口")
    public AjaxResult getListPage(@RequestBody BaseQuery query){
        try {
           BaseResult<Role> rows= roleService.getPageList(query);
            return AjaxResult.myRes().setObject(rows);
        }catch (Exception e){
            return AjaxResult.myRes().setMessage("分页查询失败"+e.getMessage()).setSuccess(false);
        }
    }
    //授权
    @PostMapping("/permission")
    @BasePermission(name = "授权接口")
    public AjaxResult handlePermission(@RequestBody RolePermissionDto dto){
        try {
             roleService.handlePermission(dto);
            return AjaxResult.myRes();
        }catch (Exception e){
            return AjaxResult.myRes().setMessage("设置授权失败"+e.getMessage()).setSuccess(false);
        }
    }
    //通过角色id回显每个角色数据类容
    @GetMapping("/permission/{id}")
    @BasePermission(name = "回显权限接口")
    public AjaxResult permissionSn(@PathVariable("id") Long id){
        try {
            List<String> permissionSns=roleService.getPer(id);
            return AjaxResult.myRes().setObject(permissionSns);
        }catch (Exception e){
            return AjaxResult.myRes().setMessage("回显失败"+e.getMessage()).setSuccess(false);
        }
    }

    //通过角色id回显每个角色菜单类容
    @GetMapping("/menu/{id}")
    @BasePermission(name = "回显菜单接口")
    public AjaxResult getRoleIdLoadMenu(@PathVariable("id") Long id){
        try {
            List<String> menusRole=roleService.getRoleIdLoadMenu(id);
            return AjaxResult.myRes().setObject(menusRole);
        }catch (Exception e){
            return AjaxResult.myRes().setMessage("回显菜单失败"+e.getMessage()).setSuccess(false);
        }
    }
    //修改角色菜单权限
    @PutMapping("/menu/add")
    @BasePermission(name = "保存修改菜单接口")
    public AjaxResult saveMenu(@RequestBody MenuDto menuDto){
        try {
            roleService.saveMenu(menuDto);
            return AjaxResult.myRes();
        }catch (Exception e){
            return AjaxResult.myRes().setMessage("修改菜单失败"+e.getMessage()).setSuccess(false);
        }
    }
    //为员工设置角色

    @PostMapping("/emp_role/update")
    @BasePermission(name = "为员工设置角色接口")
    public AjaxResult saveEmpRoleMenu(@RequestBody MenuDto menuDto){
        try {
            roleService.saveEmpRoleMenu(menuDto);
            return AjaxResult.myRes();
        }catch (Exception e){
            return AjaxResult.myRes().setMessage("修改菜单失败"+e.getMessage()).setSuccess(false);
        }
    }

    //查询对应员工角色权限
    @GetMapping("/emp_role/data/{id}")
    @BasePermission(name = "为员工设置角色接口")
    public AjaxResult getEmpBeforeRole(@PathVariable("id")Long id){
        try {
            List<Long> menuIds=roleService.getEmpBeforeRole(id);
            return AjaxResult.myRes().setObject(menuIds);
        }catch (Exception e){
            return AjaxResult.myRes().setMessage("修改菜单失败"+e.getMessage()).setSuccess(false);
        }
    }




}
